package cn.tedu_03;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public class Test2 {
    public static void main(String [] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {

        //  method();
        //method2();
        //method3();
        // method4();
        //method5();
        method6();
        //method7();
       //method8();

    }

    private static void method8() throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
//        Class c = Person.class;
//        Method m=c.getMethod("show", int.class,String.class);
//        Object obj=c.newInstance();
//        m.invoke(obj, 10,"tony");
//
                    Class c = Person.class;
                    Method m=c.getMethod("show", int.class,String.class);

                   Object obj =c.newInstance();
                        m.invoke(obj, 10,"jack");


    }

    private static void method7() throws NoSuchFieldException, IllegalAccessException, InstantiationException {
//        Class c =Person.class;
//       Field f = c.getField("age");
//        Object obj =c.newInstance();
//        f.set(obj, 100);
//        System.out.println(f.get(obj));

//            Class c = Person.class;
//           Field f= c.getField("name");
//
//           Object obj =c.newInstance();
//            f.set(obj,"jone");
//        System.out.println(f.get(obj));

           Class c = Person.class;
          Field f = c.getField("age");

          Object obj=c.newInstance();
          f.set(obj,20);
        System.out.println(f.get(obj));

    }

    private static void method6() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class c = Person.class;
        Object o=c.newInstance();
        System.out.println(o);

        Constructor con = c.getConstructor(String.class);
        Object o2=con.newInstance("jack");
        System.out.println(o2);


        Person p = (Person) o2;
        p.test();
        p.show(10,"x");

    }

    private static void method5() {
//        Class c =Person.class;
//        Field[] fs=c.getFields();
//
//        for(Field f :fs ){
//            System.out.println(f.getName());
//            System.out.println(f.getType().getName());
//        }

        Class c = Person.class;
        Field[] fs=c.getFields();

        for(Field f : fs){
            System.out.println(f.getType().getName());
            System.out.println(f.getName());
        }

    }

    private static void method4() {
//        Class c =Person.class;
//        Method[] ms=c.getMethods();
//
//        for(Method m :ms){
//            System.out.println(m.getName());
//
//            Class [] ms2=m.getParameterTypes();
//            System.out.println(Arrays.toString(ms2));
//
//        }

        Class c = Person.class;
        Method [] ms =c.getMethods();

        for(Method m : ms ){
            System.out.println(m.getName());

            Class [] ms2=m.getParameterTypes();
            System.out.println(Arrays.toString(ms2));

        }


    }

    private static void method3() throws NoSuchMethodException {
//        Class c =Person.class;
//        Constructor [] cs =c.getConstructors();
//
//        for(Constructor co : cs){
//           System.out.println(co);
//           System.out.println(co.getName());
//
//            Class [] cs2 =co.getParameterTypes();
//            System.out.println(Arrays.toString(cs2));
//        }

        Class c= Person.class;
        Constructor [] cs = c.getConstructors();

        for(Constructor co : cs){
            System.out.println(co);
            System.out.println(co.getName());
            System.out.println(co.getName().getBytes());

            Class [] cs2=co.getParameterTypes();
            System.out.println(Arrays.toString(cs2));

        }

    }

    private static void method2() {
        Class c=Person.class;
        System.out.println(c.getSimpleName());// 类名 Person
        System.out.println(c.getPackage().getName());//包名cn.tedu_03
        System.out.println(c.getName());//类的全路径cn.tedu_03.Person


    }

    private static void method() throws ClassNotFoundException {
//        Class c1 =Class.forName("cn.tedu_03.Person");
//        Class c2 =Person.class;
//        Class c3= new Person().getClass();

        Class c1=Class.forName("cn.tedu_03.Person");
        Class c2=Person.class;
        Class c3 =new Person().getClass();

    }
}
